function AddFields(oActive)
{
if (_bPreviewMode && oActive == null)
{
oActive = tblSection;
}

if (!CanAddFields(oActive))
{
return;
}



var oArgs = new Object();

oArgs.TabName				= GetCurrentTabName(oActive);
oArgs.SectionName			= GetCurrentSectionName(oActive);
oArgs.FieldsXml				= oFieldsXml;
oArgs.FieldPropertiesXml	= oPropertiesXml;
oArgs.FormXml				= oFormXml;
oArgs.bPreviewMode			= _bPreviewMode;

var oFieldObjs = openStdDlg(prependOrgName("/Tools/FormEditor/Dialogs/addfields.aspx"), oArgs, 550, _bPreviewMode ? 380 : 500);

if (oFieldObjs)
{
var oFieldObj;

var i;
var iLen = tabs.children.length;

var sTabName = oFieldObjs[0].TabName;

for (i = 0; i < iLen; i++)
{
if (tabs.children[i].innerText == sTabName)
{
tabs.children[i].click();

break;
}
}

iLen = oFieldObjs.length;

for (i = 0; i < iLen; i++)
{
oFieldObj = oFieldObjs[i];
AddFieldFormXml(oFieldObj);
}

RefreshCellHtml(oFieldObj.sTabName,oFieldObj.sSectionName,oFieldObj.sAttributeSchemaName);
}
}

function AddFieldFormXml(oFieldObj)
{
var oRows = GetRowsNode(oFieldObj.sSectionName);
var oCell;

oCell = oFormXml.createElement("cell");

oCell.setAttribute("auto", oFieldObj.bAutoExpanding.toString());
oCell.setAttribute("showlabel", oFieldObj.bShowLabel.toString());
oCell.setAttribute("locklevel", oFieldObj.bLocked == true ? "1" : "0");

oCell.appendChild(oFormXml.createElement("labels"));

oControl = oFormXml.createElement("control");
oCell.appendChild(oControl);

oControl.setAttribute("id", oFieldObj.sAttributeSchemaName);
oControl.setAttribute("classid", oFieldObj.sControlClassId);
oControl.setAttribute("datafieldname", oFieldObj.sAttributeSchemaName);
oControl.setAttribute("disabled", oFieldObj.bDisabled.toString());

AddCellToFormXml(oRows,oCell,oFieldObj.iRowSpan,oFieldObj.bSpansTwoColumns,oFieldObj.bAutoExpanding);

var oLabels = new Array(new LocalizedObj(oFieldObj.sLabel, USER_LANGUAGE_CODE));
PersistLocalizedNodes(oFormXml, "/entity/form/tabs/tab/sections/section/rows/row/cell[control/@id = '" + oFieldObj.sAttributeSchemaName + "']/labels/label", oLabels);
}

function CanUpdateField(oFormCell)
{
var oSection = oFormCell.parentNode.parentNode.parentNode;

if (oSection.getAttribute("locklevel") == "1")
{


alert(LOCID_FORMED_CHFLDLOCKEDSECTION);

return false;
}

var oTab = oSection.parentNode.parentNode;

if (oTab.getAttribute("locklevel") == "1")
{


alert(LOCID_FORMED_CHFLDLOCKEDTAB);

return false;
}

return true;
}


function UpdateField(oActive)
{
var sAttributeSchemaName	= oActive.name;
var oFormCell				= GetCellNode(sAttributeSchemaName);

if(!CanUpdateField(oFormCell))
{
return;
}

var oControl		= oFormCell.selectSingleNode("control");
var sTabName		= GetCurrentTabName(oActive);
var sSectionName	= GetCurrentSectionName(oActive);
var sLabel			= oFormCell.selectSingleNode("labels/label[@languagecode = " + USER_LANGUAGE_CODE + "]").getAttribute("description");
var oEvents			= GetEventObjsArray(oFormCell.selectNodes("events/event[@application = 'false']"));
var oParameters		= GetParameterBag(oFormCell.selectNodes("control/parameters/*"));
var oEntityField	= oFieldsXml.selectSingleNode("/entity/fields/field[@name = '" + sAttributeSchemaName + "']");
var sControlClassId = oControl.getAttribute("classid");
var bShowLabel		= true;
if(oFormCell.getAttribute("showlabel"))
{
bShowLabel		= (oFormCell.getAttribute("showlabel") == "true");
}
var bDisabled		= (oControl.getAttribute("disabled") == "true");
var bAutoExpanding	= (oFormCell.getAttribute("auto") == "true");
var bLocked			= (oFormCell.getAttribute("locklevel") == "1");
var iRowSpan		= 1;
if(oFormCell.getAttribute("rowspan"))
{
iRowSpan		= Number(oFormCell.getAttribute("rowspan"));
}
var sSectionLayout	= oFormCell.parentNode.parentNode.parentNode.getAttribute("layout");
var bSpansTwoColumns= (sSectionLayout == null || sSectionLayout == "varwidth") &&
(oActive.parentElement.cells.length == 1);

var sDataType				= _oDataTypes.unknown;
var sDataTypeFormat			= "";
var sAttributeDisplayName	= "";
var sAttributeDescription	= "";
if(oEntityField != null)
{
var oPropertiesField	= oPropertiesXml.selectSingleNode("/entity/fields/field[@name = '" + sAttributeSchemaName + "']");

sDataType				= oEntityField.getAttribute("datatype");
sDataTypeFormat      	= oPropertiesField.getAttribute("format");
sAttributeDisplayName	= oPropertiesField.selectSingleNode("displaynames/displayname[@languagecode = " + USER_LANGUAGE_CODE + "]").getAttribute("description");
sAttributeDescription	= oEntityField.getAttribute("description");
}

var oArgs = new Object();

var oFieldObj = new FieldObj(
sLabel, bShowLabel, bDisabled,
sTabName, sSectionName, bLocked,
bSpansTwoColumns, iRowSpan, bAutoExpanding,
sControlClassId,
oEvents,
sDataType, sDataTypeFormat,
sAttributeDisplayName, sAttributeSchemaName, sAttributeDescription,
oParameters
);

oArgs.oField			= oFieldObj;
oArgs.oPropertiesXml	= oPropertiesXml;
oArgs.oFormXml			= oFormXml;
oArgs.oFieldsXml		= oFieldsXml;
oArgs.bPreviewMode		= _bPreviewMode;



var sUrl = prependOrgName("/Tools/FormEditor/Dialogs/fieldprops.aspx?")		+
"mode=" + (_bPreviewMode ? "preview" : "form")	+
"&datatype=" + sDataType							+
"&format=" + sDataTypeFormat;


var iWidth	= _bPreviewMode ? 450 : 550;
var iHeight	= _bPreviewMode ? 425 : 600;


var oUpdatedFieldObj = openStdDlg(sUrl, oArgs, iWidth, iHeight, true);


if (oUpdatedFieldObj)
{

if (oUpdatedFieldObj.oEvents)
{
var iEventsLen = oUpdatedFieldObj.oEvents.length;

for (var k = 0; k < iEventsLen; k++)
{
PersistEventXml(oUpdatedFieldObj.sAttributeSchemaName, oUpdatedFieldObj.oEvents[k], k);
}
}

if(oUpdatedFieldObj.oParameters)
{
oParameters = oUpdatedFieldObj.oParameters;

var oCell = GetCellNode(oUpdatedFieldObj.sAttributeSchemaName);
var oParamsNode = oCell.selectSingleNode("control/parameters");
if(oParamsNode == null)
{
oParamsNode = oCell.ownerDocument.createElement("parameters");
oParamsNode = oCell.selectSingleNode("control").appendChild(oParamsNode);
}

for(var sKey in oParameters)
{
var oParamNode = oParamsNode.selectSingleNode(sKey);
if(oParamNode == null)
{
oParamNode = oCell.ownerDocument.createElement(sKey);
oParamNode = oParamsNode.appendChild(oParamNode);
}
oParamNode.text = oParameters[sKey];
oParamsNode.appendChild(oParamNode);
}
}


if ((oFieldObj.sTabName != oUpdatedFieldObj.sTabName) ||
(oFieldObj.sSectionName != oUpdatedFieldObj.sSectionName))
{

var oCell = GetCellNode(oUpdatedFieldObj.sAttributeSchemaName);


var oEvents = oCell.selectSingleNode("events");
var oParameters = null;

switch(sDataType)
{
case _oDataTypes.unknown:
case _oDataTypes.customer: case _oDataTypes.owner: case _oDataTypes.lookup: case _oDataTypes.partylist:
oParameters = oCell.selectSingleNode("control/parameters");
break;
}


RemoveField(oActive, false);


if (oFieldObj.sTabName != oUpdatedFieldObj.sTabName)
{
ClickTabByName(oUpdatedFieldObj.sTabName);
}


AddFieldFormXml(oUpdatedFieldObj);
oCell = GetCellNode(oUpdatedFieldObj.sAttributeSchemaName);


if ( oEvents )
{
oCell.insertBefore(oEvents, oCell.selectSingleNode("control"));
}
if(oParameters != null)
{
oCell.selectSingleNode("control").appendChild(oParameters);
}



oActive = _oActive;
}
else
{

var oCell = GetCellNode(oUpdatedFieldObj.sAttributeSchemaName);


oLabels = new Array(new LocalizedObj(oUpdatedFieldObj.sLabel, USER_LANGUAGE_CODE));
PersistLocalizedNodes(oFormXml, "/entity/form/tabs/tab/sections/section/rows/row/cell[control/@id = '" + oUpdatedFieldObj.sAttributeSchemaName + "']/labels/label", oLabels);
oCell.setAttribute("showlabel", oUpdatedFieldObj.bShowLabel.toString());


oCell.setAttribute("locklevel", oUpdatedFieldObj.bLocked ? "1" : "0");


var oControl = oCell.selectSingleNode("control");
oControl.setAttribute("disabled", oUpdatedFieldObj.bDisabled.toString());
oControl.setAttribute("classid", oUpdatedFieldObj.sControlClassId);


if (oFieldObj.bSpansTwoColumns != oUpdatedFieldObj.bSpansTwoColumns)
{
SetFieldSpan(oUpdatedFieldObj.sAttributeSchemaName, oUpdatedFieldObj.bSpansTwoColumns, oActive);
}

if(oFieldObj.iRowSpan != oUpdatedFieldObj.iRowSpan ||
oFieldObj.bAutoExpanding != oUpdatedFieldObj.bAutoExpanding)
{
oCell.setAttribute("auto", oUpdatedFieldObj.bAutoExpanding.toString());
ChangeRowSpan(oCell, oUpdatedFieldObj.sAttributeSchemaName,
oFieldObj.iRowSpan, oUpdatedFieldObj.iRowSpan,
oUpdatedFieldObj.bAutoExpanding);
}

}

RefreshCellHtml(oUpdatedFieldObj.sTabName, oUpdatedFieldObj.sSectionName, oUpdatedFieldObj.sAttributeSchemaName);
}
}


function SetFieldSpan(sAttributeSchemaName, bSpansTwoColumns, oActive)
{
var oRow;
var oCell = GetCellNode(sAttributeSchemaName);

if (bSpansTwoColumns)
{
oCell.setAttribute("colspan", "2");

var oNextCell	= oCell.nextSibling;
var oParentNode	= oCell.parentNode;
var oParentElem	= oActive.parentElement;

if (oNextCell)
{
if (oNextCell.selectSingleNode("control/@id"))
{


oRow = oFormXml.createElement("row");

oRow.appendChild(GetEmptyCellNode());
oRow.appendChild(oNextCell);

oParentNode.parentNode.insertBefore(oRow, oParentNode.nextSibling);


}
else
{


oParentNode.removeChild(oNextCell);


}
}
else
{
var oPrevCell = oCell.previousSibling;

if (oPrevCell.selectSingleNode("control/@id"))
{


oRow = oFormXml.createElement("row");

oRow.appendChild(oPrevCell);
oRow.appendChild(GetEmptyCellNode());

oParentNode.parentNode.insertBefore(oRow, oParentNode.nextSibling);


}
else
{


oParentNode.removeChild(oPrevCell);


}
}
}
else
{


oRow = GetRowNode(sAttributeSchemaName)

oRow.appendChild(GetEmptyCellNode());

oCell.removeAttribute("colspan");


}
}

function RemoveField(oActive, bCheckIsRequired)
{
var oCell = GetCellNode(oActive.name);

if (bCheckIsRequired && oCell.getAttribute("locklevel") == "1")
{

alert(LOCID_FORMED_FIELDLOCKED);
return false;
}

var oSection = oCell.parentNode.parentNode.parentNode;

if (bCheckIsRequired && oSection.getAttribute("locklevel") == "1")
{

alert(LOCID_FORMED_FIELDLOCKEDSECTION);
return false;
}

var oTab = oSection.parentNode.parentNode;

if (bCheckIsRequired && oTab.getAttribute("locklevel") == "1")
{

alert(LOCID_FORMED_FIELDLOCKEDTAB);
return false;
}

var oNodes = oSection.selectNodes("rows/row/cell/control[@id = '" + oActive.name + "']");

if (bCheckIsRequired && !IsRemovable(oNodes))
{
alert(LOCID_FORMED_FIELDREQUIRED);
return false;
}

var sfieldList = GetDependentList(oNodes,false);

if (bCheckIsRequired && sfieldList.length > 0 )
{

alert(LOCID_FORMED_FIELDDEPENDENTTITLE + "\n\n" + LOCID_FORMED_FIELDDEPENDENTHEAD + "\n\n" + sfieldList);
return false;
}

RemoveFieldXml(oActive);
RemoveFieldXmlHtml(oActive);

return true;
}

function RemoveFieldXmlHtml(oActive)
{
var oSection = oActive.parentElement.parentElement.parentElement;
RefreshSectionHtml(oSection);
}




function PersistEventXml(sFieldName, oUpdatedEventObj, iEventIndex )
{
var oEvent = oFormXml.selectSingleNode("/entity/form/tabs/tab/sections/section/rows/row/cell[control/@id = '" + sFieldName + "']/events/event[@application = 'false' and @name = '" + oUpdatedEventObj.Name + "']");

var oNewNode;
var oNode;



if (!oEvent)
{
var oEvents = oFormXml.selectSingleNode("/entity/form/tabs/tab/sections/section/rows/row/cell[control/@id = '" + sFieldName + "']/events");
if(!oEvents)
{
var oNode = oFormXml.selectSingleNode("/entity/form/tabs/tab/sections/section/rows/row/cell[control/@id = '" + sFieldName + "']");
oEvents = oFormXml.createElement("events");
oNode.insertBefore(oEvents, oNode.selectSingleNode("control"));
}

oEvent = oFormXml.createElement("event");
oEvents.appendChild(oEvent);

var oScript = oFormXml.createElement("script");
oEvent.appendChild(oScript);
oScript.appendChild(oFormXml.createCDATASection(" "));

var oDepends = oFormXml.createElement("dependencies");
oEvent.appendChild(oDepends);

var oDepend = oFormXml.createElement("dependency");
oDepends.appendChild(oDepend);

oEvent.setAttribute("name", oUpdatedEventObj.Name);
oEvent.setAttribute("application", "false");
}

oEvent.setAttribute("active", ( (1 == oUpdatedEventObj.Active) ? "true" : "false") );

var iNewLen		= IsNull(oUpdatedEventObj.Dependencies) ? 0 : oUpdatedEventObj.Dependencies.length;
var bDependsSame	= true;

oParentNode = oEvent.selectNodes("dependencies/dependency");
oParentNode.removeAll;

for (i = 0; i < iNewLen; i++)
{
oNewNode = oFormXml.createElement("dependency");
oNewNode.setAttribute("id", oUpdatedEventObj.Dependencies[i]);

oParentNode = oEvent.selectSingleNode("dependencies");

oParentNode.appendChild(oNewNode);
}

var oScript = oEvent.selectSingleNode("script");
oScript.firstChild.replaceData(0, oScript.firstChild.length, oUpdatedEventObj.Script);
return;
}

function CanAddFields(oActive)
{
if (oActive == null)
{
alert(LOCID_FORMED_MUSTSELECT);
return false;
}

var oTab;
var oSection;

switch (oActive.className)
{
case "ms-crm-Tab ms-crm-Tab-Selected":
{
oTab = GetTabNode(oActive.name);

if (!oTab.selectSingleNode("sections/section"))
{
alert(LOCID_FORMED_ADDSECTIONFIRST);

return false;
}

if (!oTab.selectSingleNode("sections/section[not(@locklevel) or @locklevel = '0']"))
{


alert(LOCID_FORMED_ALLSECTIONLOCKED);

return false;
}

break;
}
case "section":
{
oSection = GetSectionNode(oActive.name);
oTab = oSection.parentNode.parentNode;

break;
}
case "cell":
{
var oCell = GetCellNode(oActive.name);
oSection = oCell.parentNode.parentNode.parentNode;
oTab = oSection.parentNode.parentNode;
break;
}
}


if (oTab != null && oTab.getAttribute("locklevel") == "1")
{


alert(LOCID_FORMED_ADDFIELDLOCKEDTAB);

return false;
}

if (oSection != null && oSection.getAttribute("locklevel") == "1")
{


alert(LOCID_FORMED_ADDFIELDLOCKEDSEC);

return false;
}

return true;
}


function AddIFrame(oActive)
{
if (!CanAddFields(oActive))
{
return;
}

var sTabName				= GetCurrentTabName(oActive);
var sSectionName			= GetCurrentSectionName(oActive);

var oArgs = new Object();

oArgs.Field	= new IframeObj("","",false,4,false,true,sTabName,sSectionName,null,false,null,null,true);
oArgs.FieldsXml				= oFieldsXml;
oArgs.FieldPropertiesXml	= oPropertiesXml;
oArgs.FormXml				= oFormXml;
var oIframe = openStdDlg(prependOrgName("/Tools/FormEditor/Dialogs/iframe.aspx?mode=add&datatype=iframe&scrolling=auto"), oArgs, 470, 660);


if (oIframe)
{
AddIframeFormXml(oIframe);
RefreshCellHtml(oIframe.TabName,oIframe.SectionName,oIframe.Id);
}

}

function AddIframeFormXml(oIframe)
{
var oRows = GetRowsNode(oIframe.SectionName);

var oCell = oFormXml.createElement("cell");
oCell.setAttribute("showlabel", oIframe.ShowLabel.toString());
oCell.setAttribute("auto", oIframe.Auto.toString());

oCell.appendChild(oFormXml.createElement("labels"));

var oControl = oFormXml.createElement("control");
oCell.appendChild(oControl);

oControl.setAttribute("id", oIframe.Id);
oControl.setAttribute("classid", _ControlClassIds.iframe);

var oParameters = oFormXml.createElement("parameters");
oControl.appendChild(oParameters);

var oUrl = oFormXml.createElement("Url");
oParameters.appendChild(oUrl);
oUrl.text = oIframe.Url;

var oPassParams = oFormXml.createElement("PassParameters");
oParameters.appendChild(oPassParams);
oPassParams.text = oIframe.PassParams.toString();

var oRestricted = oFormXml.createElement("Security");
oParameters.appendChild(oRestricted);
oRestricted.text = oIframe.Secure.toString();

var oScrolling = oFormXml.createElement("Scrolling");
oParameters.appendChild(oScrolling);
oScrolling.text = oIframe.Scrolling;

var oBorder = oFormXml.createElement("Border");
oParameters.appendChild(oBorder);
oBorder.text = oIframe.Border.toString();

UpdateIframeDependencies(oCell,oIframe);

AddCellToFormXml(oRows,oCell,oIframe.RowSpan,true,oIframe.Auto);

PersistLocalizedNodes(oFormXml, "/entity/form/tabs/tab/sections/section/rows/row/cell[control/@id = '" + oIframe.Id + "']/labels/label", oIframe.Labels);

return true;
}


function UpdateIframeDependencies(oCell,oIframe)
{
if(oIframe.Dependencies)
{
var iLen	= oIframe.Dependencies.length;
var oEvents = oCell.selectSingleNode("events");

if(!oEvents)
{
oEvents = oFormXml.createElement("events");
oCell.appendChild(oEvents);

}
var oEvent = oEvents.selectSingleNode("event[@name='onload']");


if(!oEvent)
{
oEvent = oFormXml.createElement("event");
oEvent.setAttribute("name","onload");
oEvents.appendChild(oEvent);
var oScript = oFormXml.createElement("script");
oEvent.appendChild(oScript);
oScript.appendChild(oFormXml.createCDATASection(" "));
var oDepends = oFormXml.createElement("dependencies");
oEvent.appendChild(oDepends);
oEvent.setAttribute("application", false);
}

var oParentNode =  oEvent.selectSingleNode("dependencies");
var oChildNodes = oParentNode.selectNodes("dependency");
oChildNodes.removeAll;
var oNewNode;
for (var i = 0; i < iLen; i++)
{
oNewNode = oFormXml.createElement("dependency");
oNewNode.setAttribute("id", oIframe.Dependencies[i]);
oParentNode.appendChild(oNewNode);
}
}
}

function UpdateIFrame(oActive)
{
var sFrameName		= oActive.name;
var oFormCell			= GetCellNode(sFrameName);

if(!CanUpdateField(oFormCell))
{
return;
}

var sTabName				= GetCurrentTabName(oActive);
var sSectionName			= GetCurrentSectionName(oActive);

var oControl            = oFormCell.selectSingleNode("control");
var oParameters			= oControl.selectSingleNode("parameters");
var sScrolling			= oParameters.selectSingleNode("Scrolling").text;

var oDependsNodes = oFormCell.selectNodes("events/event[@name='onload']/dependencies/dependency");
var iDependsLen = oDependsNodes.length;
var oDependsAry = new Array(iDependsLen);

for (j = 0; j < iDependsLen; j++)
{
oDependsAry[j] = oDependsNodes[j].getAttribute("id");
}

var oArgs = new Object();

var oIframe		= new IframeObj(oControl.getAttribute("id"),
oParameters.selectSingleNode("Url").text,
oParameters.selectSingleNode("PassParameters").text == "true",
oFormCell.getAttribute("rowspan"),
oFormCell.getAttribute("auto") == "true",
oParameters.selectSingleNode("Security").text == "true",
sTabName,
sSectionName,
GetLocalizedObjsArray(oFormCell.selectNodes("labels/label")),
(oFormCell.getAttribute("showlabel") == "true"),
oDependsAry,
sScrolling,
oParameters.selectSingleNode("Border").text == "true");
oArgs.Field 				= oIframe;
oArgs.FieldsXml				= oFieldsXml;
oArgs.FieldPropertiesXml	= oPropertiesXml;
oArgs.FormXml				= oFormXml;

var oUpdatedIframe = openStdDlg(prependOrgName("/Tools/FormEditor/Dialogs/iframe.aspx?mode=edit&datatype=iframe&scrolling=" + sScrolling), oArgs, 470, 660);

if(oUpdatedIframe)
{



if ((oIframe.TabName != oUpdatedIframe.TabName) || (oIframe.SectionName != oUpdatedIframe.SectionName))
{
RemoveField(oActive, false);

if (oIframe.TabName != oUpdatedIframe.TabName)
{
ClickTabByName(oUpdatedIframe.TabName);
}

AddIframeFormXml(oUpdatedIframe);
RefreshCellHtml(oUpdatedIframe.TabName,oUpdatedIframe.SectionName,oUpdatedIframe.Id);

oActive = _oActive;


}
else
{
var oCell = GetCellNode(oUpdatedIframe.Id);

var oControl = oCell.selectSingleNode("control");
var oParameters = oControl.selectSingleNode("parameters");
oParameters.selectSingleNode("Security").text = oUpdatedIframe.Secure.toString();
oParameters.selectSingleNode("Url").text = oUpdatedIframe.Url;
oParameters.selectSingleNode("PassParameters").text = oUpdatedIframe.PassParams.toString();
oParameters.selectSingleNode("Scrolling").text = oUpdatedIframe.Scrolling;
oParameters.selectSingleNode("Border").text = oUpdatedIframe.Border.toString();

PersistLocalizedNodes(oFormXml, "/entity/form/tabs/tab/sections/section/rows/row/cell[control/@id = '" + oUpdatedIframe.Id + "']/labels/label", oUpdatedIframe.Labels);
oCell.setAttribute("showlabel", oUpdatedIframe.ShowLabel.toString());

UpdateIframeDependencies(oCell,oUpdatedIframe);

if(oIframe.RowSpan != oUpdatedIframe.RowSpan || oIframe.Auto != oUpdatedIframe.Auto)
{
oCell.setAttribute("auto",oUpdatedIframe.Auto.toString());
ChangeRowSpan(oCell,oUpdatedIframe.Id,oIframe.RowSpan,oUpdatedIframe.RowSpan,oUpdatedIframe.Auto);
}

RefreshCellHtml(oUpdatedIframe.TabName,oUpdatedIframe.SectionName,oUpdatedIframe.Id);
}



}

}

function ChangeRowSpan(oCell,sFieldName,sOldRowSpan,sNewRowSpan,bAuto)
{
var iOldRowSpan = parseInt(sOldRowSpan,10);
var iNewRowSpan = parseInt(sNewRowSpan,10);
var oRow = oCell.parentNode;
var oRows = oRow.parentNode;
var oSection = oRows.parentNode;
var layout = oSection.getAttribute("layout");
var columns = oSection.getAttribute("columns");
var sectionName = oSection.getAttribute("name");
var bRefresh = false;
if(layout == null)
{
layout = "varwidth";
columns = "11";
}

var nextSibling = oRow.nextSibling;
var oLastRow = oRow;
while(nextSibling != null && nextSibling.childNodes.length == 0)
{
oLastRow = nextSibling;
nextSibling = nextSibling.nextSibling;
}

if(layout == "varwidth")
{


if(iNewRowSpan > iOldRowSpan)
{
for(var i = 0 ; i < iNewRowSpan - iOldRowSpan ; i++)
{
oLastRow = oFormXml.createElement("row");
oRows.insertBefore(oLastRow,nextSibling);
}

}

else
{
for(var i = 0 ; i < iOldRowSpan - iNewRowSpan ; i++)
{

if(oLastRow != null && oLastRow.childNodes.length == 0 )
{
var deleteRow = oLastRow;
oLastRow = oLastRow.previousSibling;
oRows.removeChild(deleteRow);
}
}
}

if(bAuto)
{
CheckAndRemoveAuto(oSection,oCell.selectSingleNode("control").getAttribute("id"));
oLastRow.setAttribute("height", "auto");
oSection.setAttribute("height", "auto");
}
else
{
oLastRow.removeAttribute("height");
if(oSection.selectSingleNode("rows/row/cell[@auto = 'true']") == null)
{
oSection.removeAttribute("height");
}
}

}

else
{

CreateSectionSchema(sectionName,oRows,columns);
var bChanged = ChangeRowSpanInSchema(sFieldName,iOldRowSpan,iNewRowSpan);

if(bChanged)
{
RegenerateXmlFromSchema(oRows);
}
}

oCell.setAttribute("rowspan", iNewRowSpan);

}




function AddCellToFormXml(oRows,oCell,iRowSpan,bCanSpanColumn,bAuto)
{
oCell.setAttribute("rowspan", iRowSpan);
var oSection = oRows.parentNode;
var layout = oSection.getAttribute("layout");
var columns = oSection.getAttribute("columns");
var sectionName = oSection.getAttribute("name");
if(layout == null)
{
layout = "varwidth";
columns = "11";
}
var maxColumns = columns.length;
var bEmptyCell = false;

var bInserted = false;
var rowCount = 0;
var insertPosition = 0;


if(layout == "varwidth")
{
if(bCanSpanColumn)
{
oCell.setAttribute("colspan","2");

}
else
{
bEmptyCell = true;
var blankCell = oRows.selectSingleNode("row/cell[not(control/@id)]");
if(blankCell !=null)
{
insertPosition = (blankCell.nextSibling !=null) ? 0 : 1;
blankCell.parentNode.replaceChild(oCell,blankCell);
bInserted = true;
rowCount = 1;
}
oCell.setAttribute("colspan", "1");
}
if(!bInserted)
{
var oRow = oFormXml.createElement("row");
oRows.appendChild(oRow);
oRow.appendChild(oCell);

if(bEmptyCell)
{
oCell = oFormXml.createElement("cell");
oCell.appendChild(oFormXml.createElement("labels"));
oCell.setAttribute("showlabel", "true");
oCell.setAttribute("colspan" ,"1");
oRow.appendChild(oCell);
}

rowCount = 1;
}

for(var i = rowCount ; i < iRowSpan ; i++)
{
oRow = oFormXml.createElement("row");
oRows.appendChild(oRow);
}

if(bAuto)
{
CheckAndRemoveAuto(oSection,oCell.selectSingleNode("control").getAttribute("id"));
oSection.setAttribute("height","auto");
oRow.setAttribute("height", "auto");
}
}

else
{
CreateSectionSchema(sectionName,oRows,columns);

var lastRow = oRows.lastChild;
if(lastRow == null)
{
lastRow = oFormXml.createElement("row");
oRows.appendChild(lastRow);
}
lastRow.appendChild(oCell);
var bRefresh = AddCellToSchema(oCell,iRowSpan);

if(bRefresh)
{
RegenerateXmlFromSchema(oRows);
}
}

}

function GetNextCell(oRow,position)
{
var oCell = oRow.childNodes[position];
while(oCell == null && position>= 0)
{
oCell  = oRow.childNodes[position--];
}

return oCell;
}


function RemoveFieldXml(oActive)
{
var oCell = GetCellNode(oActive.name);
var oRow = oCell.parentNode;
var oRows = oRow.parentNode;
var oSection = oRows.parentNode;
var layout = oSection.getAttribute("layout");
var columns = oSection.getAttribute("columns");
var sectionName = oSection.getAttribute("name");

if(layout == null)
{
layout = "varwidth";
columns = "11";
}
var iRowSpan = GetRowSpan(oCell);
var iColSpan  = GetColSpan(oCell);


if(layout == "varwidth")
{
if(iColSpan == 2)
{

for(var i = 0 ; i < iRowSpan - 1 ; i++)
{
var nextSibling = oRow.nextSibling;
if(nextSibling != null)
{
oRows.removeChild(nextSibling);
}
}

oRows.removeChild(oRow);

if(oRows.selectSingleNode("row/cell[@auto='true']") == null)
{
oSection.removeAttribute("height");
}
}
else
{
var insertPosition = (oCell.nextSibling !=null) ? 0 : 1;
var nextRow = oRow.nextSibling;
oRow.removeChild(oCell);
var maxColumns = columns.length;

while(nextRow != null)
{
var nextCell = GetNextCell(nextRow,insertPosition);
var nextRowSpan = GetRowSpan(nextCell);

if(GetColSpan(nextCell) == 2 )
{

if(oRow != null && oRow.selectSingleNode("cell/control") == null)
{
oRows.removeChild(oRow);
oRow = null;
}
break;
}
oRow.insertBefore(nextRow.removeChild(nextCell),oRow.childNodes[insertPosition]);
for( var i = 0 ; i < nextRowSpan ; i++)
{
oRow = oRow.nextSibling;
nextRow = nextRow.nextSibling;
}
}
if(oRow != null)
{
oCell = oFormXml.createElement("cell");
oCell.appendChild(oFormXml.createElement("labels"));
oCell.setAttribute("showlabel", "true");
oCell.setAttribute("rowspan","1");
oCell.setAttribute("colspan" ,iColSpan);
oRow.insertBefore(oCell,oRow.childNodes[insertPosition]);
}

var deleteRow = oRows.lastChild;
while(deleteRow != null && deleteRow.childNodes.length == maxColumns &&
deleteRow.selectSingleNode("cell/control") == null)
{
oRows.removeChild(deleteRow);
deleteRow = oRows.lastChild;
}
}
}
else
{
CreateSectionSchema(sectionName,oRows,columns);
var bDelete = DeleteFromSchema(oActive.name,true);
if(bDelete)
{
RegenerateXmlFromSchema(oRows);
}
}
}




function CheckAndRemoveAuto(oNewSection,sNewAutoField)
{
var oTab = oNewSection.parentNode.parentNode;
var sNewSectionName = oNewSection.getAttribute("name");

var oAutoCell = oTab.selectSingleNode("sections/section/rows/row/cell[@auto = 'true' and control/@id != '" + sNewAutoField + "' ]");
if(oAutoCell!=null)
{
var oRows = oAutoCell.parentNode.parentNode;
var oSection = oRows.parentNode;
var sectionName = oSection.getAttribute("name");

oAutoCell.removeAttribute("auto");
var newRowSpan = parseInt(oAutoCell.getAttribute("rowspan"),10) - 1;
oAutoCell.setAttribute("rowspan",newRowSpan);

oRows.removeChild(oRows.selectSingleNode("row[@height = 'auto' and not(cell)]") );

if(sectionName != sNewSectionName)
{
oSection.removeAttribute("height");
RefreshCellHtml(oTab.getAttribute("name"),sectionName,null);
}
}
}

